#include "ts.h"
int ts_fd;
int flag_time =0;
struct timeval starttime,endtime;
//1、打开触摸屏
int open_ts(void)
{
	ts_fd=open("/dev/input/event0", O_RDONLY);
	if(ts_fd == -1)
	{
		printf("open ts error!\n");
		return -1;
	}

	return 0;
}

//2、获取触摸屏的坐标值
int get_ts_xy(int *ts_x, int *ts_y)
{
	//1、获取触摸屏的坐标值
	struct input_event ts_buf;
	while(1)
	{
		bzero(&ts_buf, sizeof(ts_buf));			//清空ts_buf
		read(ts_fd, &ts_buf, sizeof(ts_buf));	//堵塞函数，直到你摸了触摸屏
		//x轴的坐标值
		if(ts_buf.type==EV_ABS && ts_buf.code==ABS_X)
		{
			*ts_x =  ts_buf.value;				//蓝色边沿的开发板
			//*ts_x =  ts_buf.value/1024.0*800;	//黑色边沿的开发板
		}
		//y轴的坐标值
		if(ts_buf.type==EV_ABS && ts_buf.code==ABS_Y)
		{
			*ts_y =  ts_buf.value;				//蓝色边沿的开发板
			//*ts_y =  ts_buf.value/600.0*480;	//黑色边沿的开发板
		}	
		//触摸值
		if(ts_buf.type==EV_KEY && ts_buf.code==BTN_TOUCH)
		{
			printf("touch=%d\n",  ts_buf.value);
			if(ts_buf.value == 0)
			{
				break;
			}
		}
	}
	printf("(%d, %d)\n",  *ts_x, *ts_y);

return 0;

}


int get_ts(int *ts_x, int *ts_y,int *fx)
{
	//1、获取触摸屏的坐标值
	int flag=0;
	int x,y;
	struct input_event ts_buf;

	*fx = 0;
	while(1)
	{
		bzero(&ts_buf, sizeof(ts_buf));			//清空ts_buf
		read(ts_fd, &ts_buf, sizeof(ts_buf));	//堵塞函数，直到你摸了触摸屏
		//x轴的坐标值
		if(ts_buf.type==EV_ABS && ts_buf.code==ABS_X)
		{
			*ts_x =  ts_buf.value;				//蓝色边沿的开发板
			//*ts_x =  ts_buf.value/1024.0*800;	//黑色边沿的开发板
		}
		//y轴的坐标值
		if(ts_buf.type==EV_ABS && ts_buf.code==ABS_Y)
		{
			*ts_y =  ts_buf.value;				//蓝色边沿的开发板
			//*ts_y =  ts_buf.value/600.0*480;	//黑色边沿的开发板
		}
		//触摸值
		if(ts_buf.type==EV_KEY && ts_buf.code==BTN_TOUCH)
		{
			printf("touch=%d\n",  ts_buf.value);
			if(ts_buf.value == 0)
			{
				if(y-*ts_y >50)
				{
					*fx = 1;
				}
				if(y-*ts_y < -50)
				{
					*fx = 2;
				}
				break;
			}
			if(flag == 0&& ts_buf.value == 1)
			{
				 x= *ts_x;
				 y= *ts_y;
				 flag =1;
			}
		}
	}
	printf("(%d, %d)\n",  *ts_x, *ts_y);

return 0;

}


//timeval starttime,endtime;
double timeval_diff(struct timeval tv0, struct timeval tv1)
{
    double time1, time2;
    
    time1 = tv0.tv_sec + (tv0.tv_usec / 1000000.0);
    time2 = tv1.tv_sec + (tv1.tv_usec / 1000000.0);

    time1 = time1 - time2;
    if (time1 < 0)
        time1 = -time1;
    return time1;
}

int get_tss(int *ts_x, int *ts_y,int *fx,int *double_ts)
{
	//1、获取触摸屏的坐标值
	
	int flag=0;
	int x,y;
	double  timee =0;
	struct input_event ts_buf;
	*double_ts =0;
	*fx = 0;
	while(1)
	{
		bzero(&ts_buf, sizeof(ts_buf));			//清空ts_buf
		read(ts_fd, &ts_buf, sizeof(ts_buf));	//堵塞函数，直到你摸了触摸屏
		//x轴的坐标值
		if(ts_buf.type==EV_ABS && ts_buf.code==ABS_X)
		{
			*ts_x =  ts_buf.value;				//蓝色边沿的开发板
			//*ts_x =  ts_buf.value/1024.0*800;	//黑色边沿的开发板
		}
		//y轴的坐标值
		if(ts_buf.type==EV_ABS && ts_buf.code==ABS_Y)
		{
			*ts_y =  ts_buf.value;				//蓝色边沿的开发板
			//*ts_y =  ts_buf.value/600.0*480;	//黑色边沿的开发板
		}
		//触摸值
		if(ts_buf.type==EV_KEY && ts_buf.code==BTN_TOUCH)
		{
			printf("touch=%d\n",  ts_buf.value);
			if(ts_buf.value == 0)
			{
				if(y-*ts_y >50)
				{
					*fx = 1;			//左滑
				}
				if(y-*ts_y < -50)
				{
					*fx = 2;			//右滑
				}
				if(x-*ts_x >50)
				{
					*fx = 3;			//左滑
				}
				if(x-*ts_x < -50)
				{
					*fx = 4;			//右滑
				}
				break;
			}
			if(flag == 0&& ts_buf.value == 1)
			{
				 x= *ts_x;
				 y= *ts_y;
				 flag =1;
			}
		}
	}
	if(flag_time == 1)
	{

		if(0.3<timeval_diff(ts_buf.time,starttime))
		{
			*double_ts =1;
		}
		starttime = ts_buf.time;
	}else
	{
		starttime = ts_buf.time;
		flag_time =1;
	}

	printf("(%d, %d)\n",  *ts_x, *ts_y);

return 0;

}


//3、关闭触摸屏
int close_ts(void)
{
	close(ts_fd);
	return 0;
}